package com.fzshang.shopping.commen.util;

import cn.hutool.core.date.DateUtil;

import java.util.Date;
import java.util.Random;

/**
 * @AUTHOR 冯政尚
 * @DATE 2020/2/14 12:09
 * @DAY_NAME_FULL: 星期五
 */
public class Identification {

    /**
     * @return 生成一个唯一的标识作为微信用户标识码
     * 字符串+当前系统时间的年月日+当前系统时间的时间戳+两个随机数字+两个随机字母
     * WX 20200214 时间戳 23 ol
     */
    public static String randomUser(){
        Date date = DateUtil.date();
        String format = DateUtil.format(date, "yyyyMMdd");

        String a = "WX";
        String  c = System.currentTimeMillis()+"";
        //生成两位数的随机数
        Random random = new Random();
        int ends = random.nextInt(99);
        String d = String.format("%02d",ends);
        //随机字母
        char e =(char)(int)(Math.random()*26+97);
        char f =(char)(int)(Math.random()*26+97);
        return a+format+c+d+e+f;
    }

    //生成随机数字和字母,
    public static String getStringRandom(int length) {

        String val = "";
        Random random = new Random();

        //参数length，表示生成几位随机数
        for(int i = 0; i < length; i++) {

            String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
            //输出字母还是数字
            if( "char".equalsIgnoreCase(charOrNum) ) {
                //输出是大写字母还是小写字母
                int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
                val += (char)(random.nextInt(26) + temp);
            } else if( "num".equalsIgnoreCase(charOrNum) ) {
                val += String.valueOf(random.nextInt(10));
            }
        }
        return val;
    }

    public static void  main(String[] args) {
        Identification test = new Identification();
        //测试
        System.out.println(test.getStringRandom(8));
    }

}
